package com.example.seataaccountsys.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.seataaccountsys.domain.Account;
import com.example.seataaccountsys.service.AccountService;
import com.example.seataaccountsys.mapper.AccountMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 *
 */
@Service
@Slf4j
public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account>
implements AccountService{

//    private static final String ERROR_USER_ID = "1002";

    @Resource
    private AccountMapper accountMapper;
    
    /**
     * 扣减账户余额
     *
     * @param userId Long
     * @param money  BigDecimal
     */
    @Override
    public void decrease(Long userId, BigDecimal money) {
        log.info("------->account-service中扣减账户余额开始");
        Account account = accountMapper.selectById(userId);
        if (account==null) {
            throw new RuntimeException("account branch exception");
        }
        //已用余额
        account.setUsed(account.getUsed().add(money));
        //剩余可用额度
        account.setResidue(account.getTotal().subtract(account.getUsed()));
        accountMapper.updateById(account);

        log.info("------->account-service中扣减账户余额结束");
    }
}




